package ferp.android.views.table.controller;

import android.graphics.Rect;
import android.view.View;
import ferp.android.views.CardView;
import ferp.android.views.table.TableView;
import ferp.android.views.table.animation.MovingCard;
import ferp.core.card.Card;
import ferp.core.game.Game;

/**
 * User: olegs
 * Date: 05/01/2015 13:24
 */
public abstract class MovingController extends Controller
{
  public MovingController(TableView.Listener listener, TableView table, MovingCard mc, Rect sink)
  {
    super(listener, table);

    selected = new CardView[Game.PLAYERS];

    this.mc = mc;
    this.sink = sink;

    resetState();
  }

  @Override
  public void reset()
  {
    super.reset();

    for (int i = 0; i < selected.length; ++i)
      if (selected[i] != null)
        deselect(i, selected[i]);
  }

  public void resetState() {state = State.NONE;}

  @Override
  public void onOfferButtonClicked(View view)
  {
    if (state == State.NONE)
    {
      // move controller to 'OFFER' state to prevent animations and as consequence double processing
      state = State.OFFER;
      listener.onOfferButtonClicked();
    }
  }

  // controller's state in moving mode (no action, animation started, 'offer' dialog shown)
  protected static enum State {NONE, ANIMATION, OFFER}

  @Override
  protected int getMarkIndex(Game g, CardView cv)
  {
    Card move = g.scenario().card(g.rounds, g.current());

    if (move == null)
      return super.getMarkIndex(g, cv);

    return cv.card().hash == move.hash ? 0 : super.getMarkIndex(g, cv);
  }

  protected CardView[] selected;
  protected State state;
  protected MovingCard mc;
  protected Rect sink;
}
